(function ($) {
    'use strict';
    $('body').fadeIn(1000);

    //window.onbeforeunload = function (event) {
    //    var event = event || window.event;
    //    // 兼容IE8和Firefox 4之前的版本
    //    if (event) {
    //        event.returnValue = "确定要关闭窗口吗？";
    //    }
    //    // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
    //    return '确定要关闭窗口吗>现代浏览器？';
    //}

    fx.home = function () {

        /**
         * 初始化系统状态信息
         */
        var initStatusMessage = function () {
            var $status = $('.home-south .message');
            $status.find('.status-system').html(fx.getOS());
            $status.find('.status-browser').html(fx.getBrowser());
            $status.find('.status-screen').html(fx.getScreenResolution());
        };

        /**
         * 初始化时间
         */
        var initTime = function () {
            var _getTime = function () {
                $('.home-south .time').html(new Date().formatDateTime());
            };
            setInterval(_getTime, 1000);
        };


        /**
         * 初始化导航菜单
         */
        var initTree = function () {
            $('.home-tree').tree({
                method: 'get',
                onClick: function (node) {
                    //if (fx.getCurrentPanelId() == node.id) return;
                    $(this).tree('expand', node.target);
                    fx.loadPage(node);
                },
                onLoadSuccess: function () {
                    var panelId = fx.getCurrentPanelId();
                    if (!panelId) return;
                    var node = $(this).tree('find', panelId);
                    if (!node) return;
                    $(this).tree('expandTo', node.target);
                    $(this).tree('select', node.target);
                    fx.loadPage(node);
                }
            });
        };

        var menuAccordion = function (menus) {
            var $obj = $('#wnav');
            $obj.accordion({ animate: false, fit: true, border: false });
            $.each(menus, function () {
                var html = '<ul>';
                var temple = '<li><div><a ref="{0}" href="javascript:void(0)" rel="{1}"><span class="icon {2}">&nbsp;</span><span class="node_fx_text">{3}</span></a></div></li>';
                $.each(this.children || [], function () {
                    html += temple.format(this.id, this.url, this.iconCls, this.text);
                });
                html += '</ul>';

                $obj.accordion('add', {
                    title: this.text,
                    content: html,
                    iconCls: 'icon ' + this.iconCls,
                    border: false
                });
            });

            var panels = $obj.accordion('panels');
            $obj.accordion('select', panels[0].panel('options').title);

            $obj.find('li').click(function () {
                $obj.find('li div').removeClass('selected');
                $(this).children('div').addClass('selected');

                var link = $(this).find('a');
                var title = link.children('.node_fx_text').text();
                var url = link.attr('rel');
                var id = link.attr('ref');
                var icon = link.children('.icon').attr('class');

                fx.loadPage({
                    id: id,
                    text: title,
                    url: url,
                    iconCls: 'icon font-icon icon-globe'
                });

                //wrapper.addTab(title, url, icon);
            }).hover(function () {
                $(this).children('div').addClass('hover');
            }, function () {
                $(this).children('div').removeClass('hover');
            });
        };

        var initLogout = function () {
            $('.logout-action').click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var url = $(this).data('url');
                fx.confirm('确定要退出系统吗?', function () {
                    fx.setCurrentPanelId(null);
                    window.location.href = url;
                });
            });


            $('.xb-action').click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                var url = $(this).data('url');
                fx.confirm('确定要下班吗?', function () {
                    fx.setCurrentPanelId(null);
                    window.location.href = url;
                });
            });
             
        };

        var loadMenus = function () {
            var url = $('#wnav').data('url');
            $.ajax({
                url: url,
                method: 'post',
                success: function (result) {
                    menuAccordion(result);

                    var node = result[0].children[0];
                    //console.log(node);
                    node.iconCls = 'icon font-icon icon-globe';
                    fx.loadPage(node);

                }
            });
        };

        return {
            /**
             * 初始化主界面
             */
            init: function () {
                loadMenus();
                //initTree();
                initTime();
                initStatusMessage();
                initLogout();
                fx.monitorPanelWidth('homeWestWidth', $('body'), 'west');
            }
        };

    }();

    $(document).ready(function () {
        fx.home.init();
    });

})(jQuery);